package com.itshare.video.util.mq;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.itshare.video.common.web.WebUtil;
import com.itshare.video.mapper.UserMessageMapper;
import com.itshare.video.mapper.VideoMapper;
import com.itshare.video.mapper.custom.UserMessageMapperCustom;
import com.itshare.video.pojo.po.UserMessage;
import com.itshare.video.pojo.po.Video;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author lyr
 * @create 2020/10/25 23:12
 */
@Slf4j
@Component
public class UserMessageMq {


    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private UserMessageMapper userMessageMapper;

    @Resource
    private VideoMapper videoMapper;

    /**
     * 消息推送
     * @param userMessage
     */
    public void  sentMsg(UserMessage userMessage) {
        rabbitTemplate.convertAndSend("video_like",userMessage);
    }


    @RabbitListener(queues = "video_like")
    public void getMsg(Message msg) {
        UserMessage message = (UserMessage) msg.getPayload();
        log.info("mq msg {}",message);
        //用户视频点赞 或者视频举报
        userMessageMapper.insertSelective(message);
    }


    /**
     * 异步更新视频数量
     * @param video
     */
    public void sendMsg(Video video) {
        rabbitTemplate.convertAndSend("video_info",video);
    }

    @RabbitListener(queues = "video_info")
    public void getMsgInfoVideo(Message videoInf) {
        Video video = (Video) videoInf.getPayload();
        //异步更新
        videoMapper.updateByPrimaryKeySelective(video);

    }





}
